﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Might.ViewsTabControl {
    public partial class ViewsTabcontrolView : IViewsTabControlView {

        private readonly ViewsTabControlPresenter presenter;

        public ViewsTabcontrolView(ViewsTabControlPresenter presenter) {
            InitializeComponent();
            this.presenter = presenter;
            this.presenter.View = this;
            ViewTabControl.SelectionChanged += ViewTabControl_SelectionChanged;
        }

        #region IViewsTabControlView Members

        public event EventHandler SelectionChanged;

        public UIElement SelectedItem {
            get { return ViewTabControl.SelectedItem as UIElement; }
            set { ViewTabControl.SelectedItem = value; }
        }

        public void AddTab(UIElement tab) {
            ViewTabControl.Items.Add(tab);
            SelectedItem = tab;
        }

        public void SelectTab(UIElement tab) {
            throw new NotImplementedException();
        }
        
        public void RemoveTab( UIElement tab ) {
            if( ViewTabControl.Items.Contains( tab ) ) {
                ViewTabControl.Items.Remove( tab );
            }
        }

        #endregion

        #region Internals handlers and metohods

        protected void onSelectionChanged(EventArgs e) {
            if (SelectionChanged != null) {
                SelectionChanged(this, e);
            }
        }

        private void ViewTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            onSelectionChanged(null);
        }

        #endregion
    }
}
